package com.neusoft.hifly.push.email.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import com.neusoft.hifly.commons.exception.SystemErrorException;
import com.neusoft.hifly.core.log.Log;
import com.neusoft.hifly.core.pojo.ApiCode;
import com.neusoft.hifly.core.pojo.ApiResponse;
import com.neusoft.hifly.push.email.pojo.MailObj;
import com.neusoft.hifly.push.email.service.IEmailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * Title: 邮件服务
 * <p>
 * Description: 发送邮件接口
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
@CrossOrigin
@RestController
@RequestMapping("/api/v1/email")
@Api(value = "email-api", description = "邮件发送接口")
public class EmailApi {

	@Autowired(required = false)
	private IEmailService emailService;

	/**
	 * 邮件发送
	 *
	 * @param obj
	 *            邮件对象
	 * @return 是否成功
	 */
	@ApiOperation("发送短信")
	@ApiImplicitParams({ @ApiImplicitParam(name = "obj", value = "邮件对象", dataType = "MailObj", required = true) })
	@PostMapping(value = "/send")
	public ApiResponse<?> sendMail(@RequestBody final MailObj vo) {
		if (vo == null) {
			return new ApiResponse<>(ApiCode.ERROR.getCode(), "参数错误！");
		}
		Log.error("邮件发送：" + JSON.toJSONString(vo));

		try {
			emailService.sendMail(vo);
		} catch (final SystemErrorException e) {
			return new ApiResponse<>(e);
		}

		return new ApiResponse<>(ApiCode.SUCCESS.getCode(), "发送成功！");

	}
}
